package com.sixks.app.dao;

import com.sixks.app.domain.sku.ClothesSKU;
import com.sixks.app.domain.sku.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * <p>Title: JdbcOrderDao.java </p>
 * <p>Description: </p>
 * <p>Company: Snail Game </p>
 *
 * @author Xinying Ge
 * @date: 03/05/2014
 * @time: 18:51
 */
@Component
public class JdbcOrderDao implements OrderDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Order> loadOrderByDate(Date start, Date end) {
        final Map<Integer, Order> orderMap = new HashMap<Integer, Order>();

        List<Order> result = jdbcTemplate.query("select o.order_id,o.dateline, og.`product_id`,og.attr,og.`goods_pic_url`,og.`qty` from order_goods og inner join `order` o USING(order_id) where o.dateline >= ? and o.dateline < ?",new Object[]{start, end}, new RowMapper<Order>() {
            @Override
            public Order mapRow(ResultSet resultSet, int i) throws SQLException {
                Order order;
                if(orderMap.containsKey(resultSet.getInt(1))){
                    order = orderMap.get(resultSet.getInt(1));
                }else{
                    order = new Order();
                    order.setCreate(resultSet.getDate(2));
                    order.setId(resultSet.getInt(1));
                    orderMap.put(order.getId(), order);
                }
                ClothesSKU sku = new ClothesSKU();
                sku.setProductId(resultSet.getInt(3));
                sku.setAttr(resultSet.getString(4));
                sku.setImage(resultSet.getString(5));
                sku.setQuantity(resultSet.getInt(6));
                sku.setBelongsTo(order);
                order.getSkus().add(sku);
                return order;
            }
        });
        Set<Order> orderSet = new HashSet<Order>();
        orderSet.addAll(result);
        //
        Iterator<Order> itr = orderSet.iterator();
        List<Order> sort = new ArrayList<Order>();
        while(itr.hasNext()){
            Order tmp = itr.next();
            sort.add(tmp);
        }
        Collections.sort(sort,new Comparator<Order>() {
            @Override
            public int compare(Order o1, Order o2) {
                return o1.getId() - o2.getId();
            }
        });
        return sort;
    }

    @Override
    public List<ClothesSKU> loadSKUByDate(Date start, Date end, String prodId) {
        if(prodId == null || "".equals(prodId.trim())){
          return getSkus(start, end);
        }
        return getSkus(start, end, prodId);
    }

    private List<ClothesSKU> getSkus(Date start, Date end) {
        final Map<Integer, Order> orderMap = new HashMap<Integer, Order>();

        return jdbcTemplate.query("select o.order_id,o.dateline, og.`product_id`,og.attr,og.`goods_pic_url`,og.`qty` from order_goods og inner join `order` o USING(order_id) where o.dateline >= ? and o.dateline <= ? order by og.product_id ",new Object[]{start, end}, new RowMapper<ClothesSKU>() {
            @Override
            public ClothesSKU mapRow(ResultSet resultSet, int i) throws SQLException {
                Order order;
                if(orderMap.containsKey(resultSet.getInt(1))){
                   order = orderMap.get(resultSet.getInt(1));
                }else{
                    order = new Order();
                    order.setCreate(resultSet.getDate(2));
                    order.setId(resultSet.getInt(1));
                    orderMap.put(order.getId(), order);
                }
                ClothesSKU sku = new ClothesSKU();
                sku.setProductId(resultSet.getInt(3));
                sku.setAttr(resultSet.getString(4));
                sku.setImage(resultSet.getString(5));
                sku.setQuantity(resultSet.getInt(6));
                sku.setBelongsTo(order);
                return sku;
            }
        });
    }
    private List<ClothesSKU> getSkus(Date start, Date end, String productId) {
        final Map<Integer, Order> orderMap = new HashMap<Integer, Order>();

        return jdbcTemplate.query("select o.order_id,o.dateline, og.`product_id`,og.attr,og.`goods_pic_url`,og.`qty` from order_goods og inner join `order` o USING(order_id) where o.dateline >= ? and o.dateline <= ? and og.product_id = ? order by og.product_id ",new Object[]{start, end, productId}, new RowMapper<ClothesSKU>() {
            @Override
            public ClothesSKU mapRow(ResultSet resultSet, int i) throws SQLException {
                Order order;
                if(orderMap.containsKey(resultSet.getInt(1))){
                    order = orderMap.get(resultSet.getInt(1));
                }else{
                    order = new Order();
                    order.setCreate(resultSet.getDate(2));
                    order.setId(resultSet.getInt(1));
                    orderMap.put(order.getId(), order);
                }
                ClothesSKU sku = new ClothesSKU();
                sku.setProductId(resultSet.getInt(3));
                sku.setAttr(resultSet.getString(4));
                sku.setImage(resultSet.getString(5));
                sku.setQuantity(resultSet.getInt(6));
                sku.setBelongsTo(order);
                return sku;
            }
        });
    }
}
